import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

class HttpDemo extends StatefulWidget {
  HttpDemo({Key? key}) : super(key: key);

  @override
  State<HttpDemo> createState() => _HttpDemoState();
}

class _HttpDemoState extends State<HttpDemo> {
  List _list = [];
  String _msg = "";
  @override
  void initState() {
    super.initState();
    _getData();
  }

  _getData() async {
    //'jd.itying.com/api/httpGet'
    try {
      var apiUrl = Uri.parse('https://jd.itying.com/api/pcate');
      var response = await http.get(apiUrl);
      if (response.statusCode == 200) {
        print(json.decode(response.body));
        setState(() {
          this._list = json.decode(response.body)["result"];
        });
      } else {
        print('Response status: ${response.statusCode}');
      }
      // print('Response status: ${response.statusCode}');
      // print('Response body: ${response.body}');
      // print(response.body is String);
      // setState(() {
      //   this._msg = json.decode(response.body)["msg"];
      // });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("请求数据demo"),
      ),
      body: this._list.length > 0
          ? ListView.builder(
              itemCount: this._list.length,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text(this._list[index]["title"]),
                );
              },
            )
          : Text("没有数据"),
    );
  }
}
